package ledrpt

import grails.converters.JSON

/**
 * @Program: HIKVISION_RPT
 * @Description: 设备看板
 * @Author: 樊双峰
 * @Date: 2018-06-30 1:14
 */
class EqpBoardController {

    def dynamicQueryService

    /**
     * 获取设备信息
     * @return
     */
    def getEqpInfo() {
        params.SYS_QUERY_NAME = 'getEqpInfoJSON'
        def getEqpInfoJSON = dynamicQueryService.queryForList(params)
        render getEqpInfoJSON as JSON
    }

    /**
     * 设备图形拖拽
     * @return
     */
    def changeLocation() {
        def lmUser = params.lmUser
//        if(lmUser != '00'.trim()) {
            def id = params.id ?: 0
            def p = DwrEqpBoard.get(id)
            p.x = new BigDecimal(params.x)
            p.y = new BigDecimal(params.y)
            try {
                p.save(flush: true)
                if (p.hasErrors()) {
                    println p.errors
                }
            } catch (Exception e) {
                print(e.getMessage())
            }
//        }
        render ""
    }

    /**
     * 设备图形放大缩小
     * @return
     */
    def deformation() {
        def lmUser = params.lmUser
//        if(lmUser == 'ADMIN'.trim()) {
            def id = params.id ?: 0
            def p = DwrEqpBoard.get(id)
            if (params.act == '0') {
                p.width += 10
                p.height += 10
            } else if (params.act == '1') {
                p.width -= 10
                p.height -= 10
            }
            try {
                p.save(flush: true)
                if (p.hasErrors()) {
                    println p.errors
                }
            } catch (Exception e) {
                print(e.getMessage())
            }
//        }
    }

    /**
     * 移除设备
     * @return
     */
    def removeEqp() {
        def lmUser = params.lmUser
//        if(lmUser == 'ADMIN'.trim()) {
            def id = params.id ?: 0
            def p = DwrEqpBoard.get(id)
            p.delete()
//        }
    }

    /**
     * 保存设备
     * @param cfu
     * @return
     */
    def saveEqp(DwrEqpBoard dwrEqpBoard) {
        def lmUser = params.lmUser
//        print lmUser+'aaa'
//        if(lmUser == 'ADMIN'.trim()) {
            print lmUser+'bbb'
            dwrEqpBoard.save()
            render """{
                    "statusCode":"200",
                    "message":"${
                message(code: 'default.qcreated.message', args: [message(code: 'DwrEqpBoard.label', default: 'DwrEqpBoard'), dwrEqpBoard.id])
            }",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
//        }
    }

    /**
     * 删除设备
     * @param c
     * @return
     */
    def deleteEqp(DwrEqpBoard dwrEqpBoard) {
        def lmUser = params.lmUser
//        if(lmUser == 'ADMIN'.trim()) {
            dwrEqpBoard.delete()
            render """{
                    "statusCode":"200",
                    "message":"${
                message(code: 'default.created.message', args: [message(code: 'DwrEqpBoard.label', default: 'DwrEqpBoard'), dwrEqpBoard.id])
            }",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
//        }
    }

    /**
     * 设备信息页面
     * @return
     */
    def eqpInfo(){
        // 设置编码集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 设置分页
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startNo = (pageSize * (pageCurrent - 1)) + 1
        params.endNo = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        params.location = params.eqpStateLocationID
        def lmUser = params.lmUser
        def user= lmUser

        print("=============")
        print(user)
        // 设备
        params.SYS_QUERY_NAME = 'eqpStateEQPIDList'
        def eqpStateEQPIDList = dynamicQueryService.queryForList(params)
        // 设备信息列表
        params.SYS_QUERY_NAME = 'getEqpInfo'
        def getEqpInfo = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (getEqpInfo.size() > 0) {
            tableHeadList = getEqpInfo[0].keySet()
        }
        // 总条数
        params.SYS_QUERY_NAME = 'eqpCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        params.SYS_QUERY_NAME = 'eqpStateLocationID'
        def eqpStateLocationIDList = dynamicQueryService.queryForList(params)
        [eqpStateLocationIDList:eqpStateLocationIDList,pageSize: pageSize, totalCount: totalCount, pageSizeList: pageSizeList, eqpId: params.eqpId?:'',
         eqpStateEQPIDList:eqpStateEQPIDList, getEqpInfo:getEqpInfo, tableHeadList: tableHeadList,user: user,eqpStateEQPID:params.eqpStateEQPID?:'',eqpStateLocationID:params.eqpStateLocationID?:'']
    }

    /**
     * 增加设备页面
     * @param c
     * @return
     */
    def addEqp(DwrEqpBoard dwrEqpBoard) {
        params.SYS_QUERY_NAME = 'addEqp'
        def addEqp = dynamicQueryService.queryForList(params)
        render(view: 'addEqp',
                model: [DwrEqpBoard: dwrEqpBoard,addEqp:addEqp,])
    }

    /**
     * 修改设备页面
     * @param c
     * @return
     */
    def editEqp(DwrEqpBoard dwrEqpBoard) {
        render(view: 'editEqp', model: [DwrEqpBoard: dwrEqpBoard])
    }

    /**
     * 舍弃
     */
    def openEqpPage() {
        params.SYS_QUERY_NAME = 'getStateColor'
        def getStateColor = dynamicQueryService.queryForList(params)
        [getStateColor: getStateColor]
    }

    def showEqpBoard() {}

    def eqpBoard() {}
}